<?php
class Logic_Module extends CMF_Krypton_Core_Logic
{
	public static $fileTypes	= array(
		'classes'	=> 'classes/',
		'templates'	=> 'contents/templates/',
		'forms'		=> 'contents/forms/',
		'locales'	=> 'contents/languages/',
	);

	public static $pathModuleConfigs	= 'config/modules/';

	public static function addConfig( $moduleKey, $data )
	{
		if( !is_string( $moduleKey ) || empty( $moduleKey ) )
			throw new LogicException( 'module_key_invalid' );
		if( self::exists( $moduleKey ) )
			throw new LogicException( 'module_already_existing' );
		$template	= array(
			'id'			=> $moduleKey,
			'version'		=> isset( $data['version'] ) ? $data['version'] : '',
			'label'			=> isset( $data['label'] ) ? $data['label'] : '',
			'description'	=> isset( $data['description'] ) ? $data['description'] : '',
			'author'		=> isset( $data['author'] ) ? $data['author'] : '',
			'company'		=> isset( $data['company'] ) ? $data['company'] : '',
			'relations'		=> array(
				'mandatory'	=> array(),
				'optional'	=> array(),
			),
			'files'			=> array(
				'classes'	=> array(),
				'forms'		=> array(),
				'html'		=> array(),
				'languages'	=> array(),
				'templates'	=> array(),
				'images'	=> array(),
			),
			'sql'			=> array(
				'install'	=> "",
				'uninstall'	=> "",
			),
			'config'		=> array(),
			'pages'			=> array(),
		);
		$config	= self::mergeConfigData( $template, $data );
		return self::saveConfig( $moduleKey, $config );
	}

	public static function createArchiveFromModule( $moduleKey )
	{
		$config	= self::getConfig( $moduleKey );

		$fileArc	= $moduleKey.'.tar';
		@unlink( $fileArc );
		$arc	= new File_Arc_Tar();
		$arc->addFile( self::$pathModuleConfigs.$moduleKey.'.json' );
		foreach( self::$fileTypes as $type => $path )
		{
			if( !empty( $config['files'][$type] ) )
			{
				foreach( $config['files'][$type] as $fileName )
				{
					$arc->addFile( $path.$fileName );
				}
			}
		}
		return $arc->save( $fileArc );
	}

	public static function exists( $moduleKey )
	{
		$filePath	= self::getFilePathFromModuleKey( $moduleKey );
		return file_exists( $filePath );
	}

	public static function getFilePathFromModuleKey( $moduleKey )
	{
		if( !is_string( $moduleKey ) || empty( $moduleKey ) )
			throw new InvalidArgumentException( 'module_key_invalid' );
		return self::$pathModuleConfigs.$moduleKey.'.json';
	}

	public static function getInstalledModules()
	{
		$list	= array();
		$regExp	= '/\.json$/';
		$index	= new File_RegexFilter( self::$pathModuleConfigs, $regExp );
		foreach( $index as $module )
		{
			$moduleKey	= pathinfo( $module->getFilename(), PATHINFO_FILENAME );
			$config		= self::getConfig( $moduleKey );
			$list[]		= array(
				'id'		=> $config['id'],
				'version'	=> $config['version'],
				'label'		=> $config['label'],
			);
		}
		return $list;
	}

	public static function getConfig( $moduleKey )
	{
		$filePath	= self::getFilePathFromModuleKey( $moduleKey );
		if( !file_exists( $filePath ) )
			throw new RuntimeException( 'module "'.$moduleKey.'" not installed' );
		return File_JSON_Reader::load( $filePath, TRUE );
	}

	protected static function mergeConfigData( $base, $data )
	{
		foreach( $data as $key => $value )
		{
			if( is_array( $value ) )
				foreach( $value as $subkey => $subvalue )
					$base[$key][$subkey]	= $subvalue;
			else
				$base[$key]	= $value;
		}
		return $base;
	}

	public static function editConfig( $moduleKey, $data )
	{
		$config	= self::getConfig( $moduleKey );
		$config	= self::mergeConfigData( $config, $data );
		if( $config !== self::getConfig( $moduleKey ) )
			return self::saveConfig( $moduleKey, $config );
		return 0;
	}

	public function installModuleFromArchive( $filePath )
	{
		$moduleKey	= pathinfo( $filePath, PATHINFO_FILENAME );
		$arcExt		= pathinfo( $filePath, PATHINFO_EXTENSION );

		if( !file_exists( $filePath ) )
			throw new RuntimeException( 'archive not available' );
		$arc	= new File_Arc_Tar( $filePath );
		chdir( 'test' );
		$arc->extract();

	}

	public static function uninstallModule( $moduleKey )
	{
		$config	= self::getConfig( $moduleKey );
		var_dump( $config );

		foreach( self::$fileTypes as $type => $path )
		{
			if( !empty( $config['files'][$type] ) )
			{
				foreach( $config['files'][$type] as $fileName )
				{
					echo "removing: .".$fileName."\n";
				}
			}
		}
	}

	public static function saveConfig( $moduleKey, $config )
	{
		if( !is_string( $moduleKey ) || empty( $moduleKey ) )
			throw new InvalidArgumentException( 'Module key must be a valid string' );
		if( !is_array( $config ) || empty( $config ) )
			throw new InvalidArgumentException( 'Config must be array' );

		$fileName	= self::$pathModuleConfigs.$moduleKey.'.json';
		File_JSON_Writer::save( $fileName, $config, TRUE );
	}
}
?>
